package com.wbao.dianniu.ui;

import java.util.ArrayList;
import java.util.List;

import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.umeng.analytics.MobclickAgent;
import com.wbao.dianniu.adapter.ShieldAdapter;
import com.wbao.dianniu.data.ShieldData;
import com.wbao.dianniu.listener.IMyshieldListener;
import com.wbao.dianniu.listener.IShildAccountListener;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.manager.MyShieldManager;
import com.wbao.dianniu.manager.ShildAccountManager;
import com.wbao.dianniu.R;

/**
 * 我的屏蔽
 * 
 * @author yejianjun
 *
 */
public class MyShieldActivity extends BaseActivity implements IMyshieldListener,IShildAccountListener{
	
	private ListView mListView;
	private List<ShieldData> mList;
	private ShieldAdapter adapter;
	private MyShieldManager manager;
	private Dialog dialog;
	private ShildAccountManager saManager;
	private int position;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentLayout(R.layout.my_shield);
		setTitleName("我的屏蔽");
		init();
		manager = new MyShieldManager(this);
		manager.addMyShieldListener(this);
		manager.shieldList(GlobalContext.accountId, 0, 999);
		saManager = new ShildAccountManager(this);
		saManager.addShildAccountListener(this);
	}

	private void init() {
		mListView = (ListView) findViewById(R.id.shield_listview);
		TextView tv = (TextView) findViewById(R.id.shield_remark);
		tv.setText(getResources().getString(R.string.shield_remark));
		adapter = new ShieldAdapter(this);
		mListView.setAdapter(adapter);
		
		mListView.setOnItemLongClickListener(longClickListener);
	}

	@Override
	public void onMyShieldFailure(int errorCode, String errorMsg) {
	}

	@Override
	public void onMyShieldSuccess(List<ShieldData> data) {
		if(null == data){
			mList = new ArrayList<>();
		}else{
			mList = data;
			adapter.setData(mList);
			adapter.notifyDataSetChanged();
		}
	}
	
	OnItemLongClickListener longClickListener = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
			MyShieldActivity.this.position = position;
			ShowDeletedDialog("取消屏蔽该用户？", position);
			return true;
		}
	};
	
	private void ShowDeletedDialog(String msg,final int position) {
		dialog = new Dialog(this, R.style.custom_Dialog);
		Window win = dialog.getWindow();
		win.getDecorView().setPadding(3, 3, 3, 3);
		win.setGravity(Gravity.CENTER);
		View view = LayoutInflater.from(this).inflate( R.layout.custom_message_dialog, null);
		Button okBtn = (Button) view.findViewById(R.id.custom_msg_ok);
		Button cancelBtn = (Button) view.findViewById(R.id.custom_msg_cancel);
		TextView message = (TextView) view.findViewById(R.id.custom_msg_message);
		message.setText(msg);
		dialog.setContentView(view);
		dialog.setCancelable(true);
		dialog.show();
		okBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
				saManager.shildAccount(GlobalContext.accountId, mList.get(position).getAccountId());
			}
		});
		cancelBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (null != dialog && dialog.isShowing()) {
					dialog.dismiss();
				}
			}
		});

	}

	@Override
	public void onShildAccountFailure(int errorCode, String errorMsg) {
	}

	@Override
	public void onShildAccountSuccess() {
		mList.remove(position);
		adapter.notifyDataSetChanged();
	}
	public void onResume() {
		super.onResume();
		MobclickAgent.onResume(this);
		}
		public void onPause() {
		super.onPause();
		MobclickAgent.onPause(this);
		}
	
}
